/**
 * 注册
 */
import { useReqBody, useReqIp, generateUser } from "@sker/runtime";
import { useTransaction, HttpError, randomSalt, encryptPassword, toSuccess } from "@sker/core";
import { SysUser } from "@sker/entities";
import { DependencyContainer } from "tsyringe";

import { z } from "zod";
const input = z.object({
    username: z.string(),
    password: z.string(),
    captcha: z.string(),
    captchaKey: z.string()
})
export default async (injector: DependencyContainer) => {
    const body = input.parse(useReqBody())
    await useTransaction(async manager => {
        const sysUser = await manager.findOne(SysUser, {
            where: { username: body.username },
            select: {
                uid: true
            }
        });
        if (sysUser) throw new HttpError(`账户已存在`)
        // 加密password
        const user = await generateUser(body.username, body.password)
        await manager.insert(SysUser, user)
        return;
    })
    return toSuccess({})
}
